﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Globalization;
using System.Runtime.InteropServices;

namespace DALC
{
    public class GlobalDALC
    {

        public DataTable dt;
        public DataSet ds;

        public StringBuilder strSQL;


        public DbConnection DbConn;
        public DbCommand DbCmd;
        public DbDataReader DbReader;
        public DbDataAdapter DbAdapter;
        public DbParameter DbParam;

        public String DbAmbiente;

        
        //'Rotina utilizada para receber uma data no formato DD/MM/YYYY e retornar no formato para gravação 'De acordo com as configurações regionais da máquina e do Banco de Dados utilizado.
        public String DateTo(String strDate, Boolean strSysdate, Int32 DbActive)
        {
            try
            { 
                String Delimitador=String.Empty;
                String ConfigDate =String.Empty;
                String strDateAux =String.Empty;
                String strDateAnt = String.Empty;

                String sRet=String.Empty;

                if(String.IsNullOrEmpty(strDate) || String.IsNullOrEmpty(strDate.ToString().Trim()))
                {
                    return sRet= "NULL";
                }
                
                if(strSysdate==true)
                {
                    switch (DbActive)
                    {
                        //Oracle
                        case 1:
                            sRet = "TRUNC(GETDATE())";
                            break;

                        //Access
                        case 2:
                            //sRet = "'" + Format(Now, "dd/mm/yyyy") + "'";
                            break;

                        //SQL SERVER
                        case 3:
                            sRet = "GETDATE()";
                            break;
                    }   
                    return sRet;
                }
                
                switch (DbActive)
                {
                    //Oracle
                    case 1:
                        sRet = " TO_DATE('" + DateTime.Parse(strDate.ToString()).ToString("dd/mm/yyyy") + "','dd/mm/yyyy')";
                        break;

                    //Access
                    case 2:
                        sRet= "'" + DateTime.Parse(strDate.ToString()).ToString("dd/mm/yyyy") + "'";
                        break;

                    //SQL SERVER
                    case 3:
                        sRet= "'" + DateTime.Parse(strDate.ToString()).ToString("yyyy/MM/dd") + "'";
                        break;
                }

                return sRet;
            }
            catch (Exception ex)
            { throw ex; }
        }

    

    }
}
